package com.ruoyi.project.system.question.controller;

import java.util.List;

import com.ruoyi.project.system.question.service.IExamRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.question.domain.ExamRecord;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;

/**
 * 答题记录Controller
 * 
 * @author ruoyi
 * @date 2023-07-15
 */
@Controller
@RequestMapping("/system/question/record")
public class ExamRecordController extends BaseController
{
    private String prefix = "system/question/record";

    @Autowired
    private IExamRecordService examRecordService;

    @RequiresPermissions("system:question:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询答题记录列表
     */
    @RequiresPermissions("system:question:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamRecord examRecord)
    {
        startPage();
        List<ExamRecord> list = examRecordService.selectExamRecordList(examRecord);
        return getDataTable(list);
    }

    /**
     * 导出答题记录列表
     */
    @RequiresPermissions("system:question:record:export")
    @Log(title = "答题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamRecord examRecord)
    {
        List<ExamRecord> list = examRecordService.selectExamRecordList(examRecord);
        ExcelUtil<ExamRecord> util = new ExcelUtil<ExamRecord>(ExamRecord.class);
        return util.exportExcel(list, "答题记录数据");
    }

    /**
     * 新增答题记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存答题记录
     */
    @RequiresPermissions("system:question:record:add")
    @Log(title = "答题记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamRecord examRecord)
    {
        return toAjax(examRecordService.insertExamRecord(examRecord));
    }

    /**
     * 修改答题记录
     */
    @RequiresPermissions("system:question:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamRecord examRecord = examRecordService.selectExamRecordById(id);
        mmap.put("examRecord", examRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存答题记录
     */
    @RequiresPermissions("system:question:record:edit")
    @Log(title = "答题记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamRecord examRecord)
    {
        return toAjax(examRecordService.updateExamRecord(examRecord));
    }

    /**
     * 删除答题记录
     */
    @RequiresPermissions("system:question:record:remove")
    @Log(title = "答题记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examRecordService.deleteExamRecordByIds(Convert.toLongArray(ids)));
    }
}